package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import model.Chengg;
import util.Pagination;
import db.DBConnect;

public class ChenggControl {
	public static Pagination pageInstance = null;
	
	/**
	 * 增加
	 * @author dujuan
	 * @return
	 * 2012-02-23
	 */
	public boolean insertChengg(Chengg chengg)
	{
		DBConnect dbc = new DBConnect();
		String sql ="insert into chengg (jingpName,jingptu,jingpNeirong) values (?,?,?)";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1,chengg.getJingpName());
			dbc.setString(2,chengg.getJingptu());
			dbc.setString(3,chengg.getJingpNeirong());
					
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally {
			dbc.close();
		}
	}
	
	/*判断数据库里面是否存在用户*/
	public static boolean isChengg(String a,String b,String c)
	{
		DBConnect dbc = new DBConnect();
		String sql ="select * from chengg where jingpname = ? and jingptu=? and jingpNeirong=?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1,a);
			dbc.setString(2,b);
			dbc.setString(3,c);
			ResultSet rs= dbc.executeQuery();
			if(rs.next()){
				return true;
			}
			else{
				return false;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			dbc.close();
		}return false;
	}
	
	
	/**
	 * 根据id获取信息
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public Chengg GetChengg(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from chengg where jingpId = ?";

		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			ResultSet rs = dbc.executeQuery();
			Chengg chengg = new Chengg();
			
			if(rs.next())
			{
				chengg.setJingpName(rs.getString("jingpName"));
				chengg.setJingptu(rs.getString("jingptu"));
				chengg.setJingpNeirong(rs.getString("jingpNeirong"));
				
						
				return chengg;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		return null;
	}
	
	
	/**
	 * 删除
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean deleteChengg(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "delete from chengg where jingpId =?";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
		
	}
	
	/**
	 * 修改
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean updateChengg (Chengg chengg, int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "update chengg set jingpName=?, jingptu=? ,jingpNeirong=? where jingpId =?";
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, chengg.getJingpName());
			dbc.setString(2, chengg.getJingptu());
			dbc.setString(3, chengg.getJingpNeirong());
			dbc.setInt(4, id);
			
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	
	
	/**
	 * 列表返回显示总结
	 * 
	 */
	
	public Collection GetChengg()
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from chengg order by jingpId desc";
		dbc.prepareStatement(sql);
		Collection<Chengg>chenggList = new ArrayList<Chengg>();
		try {
			ResultSet rs = dbc.executeQuery();
			while(rs.next())
			{
				
				Chengg  chengg = new Chengg();
				chengg.setJingpId(rs.getInt("jingpId"));
				chengg.setJingpName(rs.getString("jingpName"));
				chengg.setJingptu(rs.getString("jingptu"));
				chengg.setJingpNeirong(rs.getString("jingpNeirong"));

			}
			return chenggList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		
		return null;
	}
	


	/*
	 * 返回总结列表，分页显示
	 */
	
	public Collection GetChengg(int page, int rowsPerPage)
	{
		String sql = "select * from chengg order by jingpId desc";
		pageInstance = new ChenggControlPage(sql,rowsPerPage);
		
		try {
			return pageInstance.getPage(page);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;

	}
	

class ChenggControlPage extends Pagination{
	public ChenggControlPage(String sql, int rowsPerPage)
	{
		super(sql,rowsPerPage,1);
	}
	
	@Override
	protected Collection<Chengg> packResultSet(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		Collection<Chengg>chenggList = new ArrayList<Chengg>();
		while(rs.next())
		{
			Chengg chengg = new Chengg();
			
			chengg.setJingpId(rs.getInt("jingpId"));	
			chengg.setJingpName(rs.getString("jingpName"));
			chengg.setJingptu(rs.getString("jingptu"));
			chengg.setJingpNeirong(rs.getString("jingpNeirong"));
			
			chenggList.add(chengg);
			chengg = null;
			}
			return  chenggList;
			
		}
	
	}
	
}
	


	
	
	

	

	
	
